package sm.dam;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import sm.dam.api.DDLAdaptor;

public abstract class AbstractDDLAdaptor implements DDLAdaptor {

	@Override
	public boolean isBLob(String type) {
		boolean isLob = false;
		if (type.indexOf("BLOB") != -1) {
			isLob = true;
		} else if (type.indexOf("RAW") != -1) {
			isLob = true;
		} else if (type.indexOf("IMAGE") != -1) {
			isLob = true;
		}
		return isLob;
	}

	@Override
	public boolean isCLob(String type) {
		boolean isLob = false;
		if (type.indexOf("CLOB") != -1) {
			isLob = true;
		} else if (type.indexOf("TEXT") != -1) {
			isLob = true;
		}
		return isLob;
	}

	@Override
	public boolean isSameType(String type1, String type2) {
		if (type1 != null && type2 != null) {
			return trimSize(type1).equals(trimSize(type2));
		} else {
			return false;
		}
	}

	private String trimSize(String sqlType) {
		String dbTypeNoSize = sqlType;
		int sizeStart = sqlType.indexOf("(");
		if (sizeStart != -1) {
			dbTypeNoSize = sqlType.substring(0, sizeStart);
		}
		return dbTypeNoSize;
	}

	@Override
	public boolean isBigger(String type1, String type2) {
		int sizeStart1 = type1.indexOf("(");
		int sizeStart2 = type2.indexOf("(");
		if (sizeStart1 == -1 || sizeStart2 == -1) {
			return false;
		} else {
			int sizeEnd1 = type1.indexOf(")");
			int sizeEnd2 = type2.indexOf(")");
			String sizeStr1 = type1.substring(sizeStart1 + 1, sizeEnd1);
			String sizeStr2 = type2.substring(sizeStart2 + 1, sizeEnd2);
			int size1 = Integer.parseInt(sizeStr1);
			int size2 = Integer.parseInt(sizeStr2);
			if (size1 > size2) {
				return true;
			} else {
				return false;
			}
		}
	}

	@Override
	public String generateDropTable(String tableName) {
		return "DROP TABLE " + tableName;
	}

	@Override
	public String generateAddColumn(String tableName, String columnName,
			String dataType) {
		return "ALTER TABLE " + tableName + " ADD "
				+ toSqlEntityName(columnName) + " " + dataType;
	}

	@Override
	public String generateRenameTable(String oldTableName, String newTableName) {
		return "ALTER TABLE " + oldTableName + " RENAME TO " + newTableName;
	}

	@Override
	public String generateRenameIndex(String oldIndexName, String newIndexName) {
		return "ALTER INDEX " + oldIndexName + " RENAME TO " + newIndexName;
	}

	@Override
	public String generateDropIndex(String indexName) {
		return "DROP INDEX " + indexName;
	}

	@Override
	public String generateCreateTable(String tableName,
			Map<String, String> dbColumnNames) {
		StringBuilder builder = new StringBuilder();
		builder.append("CREATE TABLE ").append(tableName).append(" (\n");
		int i = 0;
		int size = dbColumnNames.size();
		for (Entry<String, String> entry : dbColumnNames.entrySet()) {
			String sqlFieldName = entry.getKey();
			String sqlDataType = entry.getValue();
			builder.append(toSqlEntityName(sqlFieldName) + " " + sqlDataType);
			if (i < size - 1) {
				builder.append(",");
			}
			builder.append("\n");
			i++;
		}
		builder.append(")\n");
		return builder.toString();
	}

	@Override
	public String generateCreateIndex(String indexName, boolean isUnique,
			String tableName, List<String> dbColumnNames) {
		StringBuilder builder = new StringBuilder();
		builder.append("CREATE ");
		if (isUnique) {
			builder.append("UNIQUE ");
		}
		builder.append("INDEX ").append(indexName);
		builder.append(" ON ").append(tableName).append(" (\n");

		int size = dbColumnNames.size();
		for (int i = 0; i < size; i++) {
			String dbColumnName = dbColumnNames.get(i);
			builder.append(toSqlEntityName(dbColumnName));
			if (i < size - 1) {
				builder.append(",");
			}
		}
		builder.append(")\n");
		return builder.toString();
	}

}
